import axios from "axios";
import Storage from "@/utils/Storage";

class fyRequest {
    constructor(config) {
        this.instance = axios.create(config)
        // 请求拦截器
        this.instance.interceptors.request.use(
            (config) => {
                const token = Storage.get("TOKEN")
                if (config.headers && token) {
                    // 当我们的token有值时 我们就携带token
                    config.headers.Authorization = 'Bearer ' + token
                }
                return config
            }, (err) => {
                return Promise.reject(err);
            })
        // 响应拦截器
        this.instance.interceptors.response.use(
            res => {

                return res.data
            },
            error => {
                alert(error.response.data)
                // ElNotification({
                //     title: 'Error',
                //     message: error.response.data,
                //     type: 'error',
                // })
                let message = ''
                switch (error.response.status) {
                    case 400:
                        message = "请求错误(400)";
                        break;
                    case 401:
                        message = "未授权，请重新登录(401)";
                        // 1.这里可以做清空storage并跳转到登录页的操作
                        // Storage.clear()
                        // 2.发送请求 获取token 在存储token 实现用户的xxx

                        break;
                    case 403:
                        message = "拒绝访问(403)";
                        break;
                    case 404:
                        message = "请求出错(404)";
                        break;
                    case 408:
                        message = "请求超时(408)";
                        break;
                    case 500:
                        message = "服务器错误(500)";
                        break;
                    case 501:
                        message = "服务未实现(501)";
                        break;
                    case 502:
                        message = "网络错误(502)";
                        break;
                    case 503:
                        message = "服务不可用(503)";
                        break;
                    case 504:
                        message = "网络超时(504)";
                        break;
                    case 505:
                        message = "HTTP版本不受支持(505)";
                        break;
                    default:
                        // eslint-disable-next-line @typescript-eslint/no-unused-vars
                        message = `连接出错(${error.response.status})!`;
                }
                return Promise.reject(error)
            })

        if (config?.interceptors) {
            this.instance.interceptors.request.use(config.interceptors.requestSuccessFn, config.interceptors.requestFailureFn)
            this.instance.interceptors.response.use(config.interceptors.responseSuccessFn, config.interceptors.responseFailureFn)
        }
    }

    request(config) {
        return this.instance.request(config)
    }

    post(config) {
        return this.request({ ...config, method: 'POST' })
    }

    get(config) {
        return this.request({ ...config, method: 'GET' })
    }

    put(config) {
        return this.request({ ...config, method: 'PUT' })
    }

    delete(config) {
        return this.request({ ...config, method: 'DELETE' })
    }

    patch(config) {
        return this.request({ ...config, method: 'PATCH' })
    }
}

export default fyRequest
